<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>深拷贝</title>
	</head>
	<body>
		<script>
			const liLei = {
				name: 'lilei',
				age: 28,
				habits: ['coding', 'hiking', 'running']
			}
			function deepClone(obj) {
				// 如果是 值类型 或 null，则直接return
				if(typeof obj !== 'object' || obj === null) {
					return obj
				}
				// 定义结果对象
				let copy = {}
				// 如果对象是数组，则定义结果数组
				if(obj.constructor === Array) {
					copy = []
				}
				// 遍历对象的key
				for(let key in obj) {
					// 如果key是对象的自有属性
					if(obj.hasOwnProperty(key)) {
						// 递归调用深拷贝方法
						copy[key] = deepClone(obj[key])
					}
				}
				return copy
			} 
			// const liLeiCopy = JSON.parse(JSON.stringify(liLei))
			const liLeiCopy = deepClone(liLei);
			liLeiCopy.habits.splice(0, 1);
			console.log('李雷副本的habits数组是', liLeiCopy.habits)
			console.log('李雷的habits数组是',  liLei.habits)
		</script>
	</body>
</html>
